/*
 * Copyright 2011-2012 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.sabayframework.activity.impl;

import org.sabayframework.activity.Activity;
import org.sabayframework.activity.ActivityContext;

/**
 * TimeActivity may be scheduled for one-time execution, or for repeated
 * execution at regular intervals.
 * @author Alex Antonau
 * */
public abstract class TimerActivity<I, O> implements Activity<I, O> {

	private final long period;
	private long nextRun;

	/**
	 * Schedules this activity for repeated fixed-delay execution, beginning
	 * after the specified delay after submission to an execution queue.
	 * Subsequent executions take place at approximately regular intervals
	 * separated by the specified period. In fixed-delay execution, each
	 * execution is scheduled relative to the actual execution time of the
	 * previous execution. If an execution is delayed for any reason (such as
	 * garbage collection or other background activity), subsequent executions
	 * will be delayed as well. In the long run, the frequency of execution will
	 * generally be slightly lower than the reciprocal of the specified period
	 * @param delay - delay in milliseconds before activity is to be executed.
	 * @param period - time in milliseconds between successive activity executions.
	 * @throws IllegalArgumentException - if delay is negative, or delay + System.currentTimeMillis() is negative.
	 * */
	public TimerActivity(long delay, long period) {
		super();
		if(delay < 0) throw new IllegalArgumentException("Incorrect delay: "+delay);
		this.period = period;
		nextRun = System.currentTimeMillis() + delay;
	}
	/**
	 * Schedules this activity for execution after the specified delay.
	 * */
	public TimerActivity(long delay) {
		this(delay, -1);
	}
	
	@Override
	public final void execute(ActivityContext<I, O> ctx) throws Exception {
		try{
			scheduledRun(ctx);
		} finally {
			nextRun = period < 0? 0 : System.currentTimeMillis() + period;
		}
	}
	
	/**
	 * Implement the action to be performed by this activity.
	 * */
	public abstract void scheduledRun(ActivityContext<I, O> ctx) throws Exception;

	@Override
	public void beforeExecute(ActivityContext<I, O> ctx) throws Exception {
	}

	@Override
	public void afterExecute(ActivityContext<I, O> ctx) throws Exception {
	}

	@Override
	public boolean isReady(ActivityContext<I, O> ctx) throws Exception {
		return System.currentTimeMillis()-nextRun >= 0;
	}

	@Override
	public boolean isDone(ActivityContext<I, O> ctx) throws Exception {
		return nextRun == 0;
	}

}
